package zq.cn.defview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import util.Globals;
import zq.cn.activity.MainActivity;
import zq.cn.dao.PageDao;

/**
 * Created by ZQ on 2016/4/21.
 */
public class MyView extends View {

    private String content;
    private MainActivity a;
    //页面坐标
    private float startX;
    private float endX;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {

        super(context, attrs);
        a = (MainActivity)context;
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN) {
                    startX = event.getX();
                }else if(event.getAction()==MotionEvent.ACTION_MOVE) {
                    endX = event.getX();
                    postInvalidate();
                }else if(event.getAction()== MotionEvent.ACTION_UP) {
                    if(Math.abs(endX-startX)>=Globals.SCREEN_WIDTH/3) {
                        //last page
                        if(endX>startX) {
                            if(a.getNowpage()>1) {
                                a.setNowpage(a.getNowpage()-1);
                                changeData();
                            }
                        }else {
                            //next page
                            if(a.getNowpage()<a.getPageCount()) {
                                a.setNowpage(a.getNowpage()+1);
                                changeData();
                            }
                        }
                    }
                    startX = 0;
                    endX = 0;
                }

                return false;
            }
        });
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        Typeface font = Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD);
        paint.setColor(Color.BLACK);
        paint.setTypeface(font);
        paint.setTextSize(Globals.CHAR_SIZE);
        if(content!=null) {
            String[] cont = content.split(Globals.KEYWORD);
            for (int i=0;i<cont.length;i++) {
                for (int j=0;j<cont[i].length();j++) {
                    if(j+i*Globals.CHARCOUNT_LINE<content.length()) {
                        canvas.drawText(String.valueOf(cont[i].charAt(j)),
                                j * (Globals.CHAR_SIZE + Globals.CHAR_AND_CHAR) + Globals.PAGE_AND_PAGE+(endX-startX),
                                (i+1) * (Globals.CHAR_SIZE + Globals.PAGE_AND_PAGE), paint);
                    }
                }
            }

            if(startX<endX && lastPage!=null) {
                cont = lastPage.split(Globals.KEYWORD);
                for (int i=0;i<cont.length;i++) {
                    for (int j=0;j<cont[i].length();j++) {
                        if(j+i*Globals.CHARCOUNT_LINE<content.length()) {
                            canvas.drawText(String.valueOf(cont[i].charAt(j)),
                                    j * (Globals.CHAR_SIZE + Globals.CHAR_AND_CHAR) + Globals.PAGE_AND_PAGE+(endX-startX)-Globals.SCREEN_WIDTH,
                                    (i+1) * (Globals.CHAR_SIZE + Globals.PAGE_AND_PAGE), paint);
                        }
                    }
                }
            }

            if(startX>endX && nextPage!=null) {
                cont = nextPage.split(Globals.KEYWORD);
                for (int i=0;i<cont.length;i++) {
                    for (int j=0;j<cont[i].length();j++) {
                        if(j+i*Globals.CHARCOUNT_LINE<content.length()) {
                            canvas.drawText(String.valueOf(cont[i].charAt(j)),
                                    j * (Globals.CHAR_SIZE + Globals.CHAR_AND_CHAR) +
                                            Globals.PAGE_AND_PAGE+(endX-startX)+Globals.SCREEN_WIDTH,
                                    (i+1) * (Globals.CHAR_SIZE + Globals.PAGE_AND_PAGE), paint);
                        }
                    }
                }
            }
        }

    }

    private String lastPage;
    private String nextPage;
    public void changeData() {
        content = PageDao.getTxtContent((int) a.getMap().get("txtId"), a.getNowpage());
        if(a.getNowpage()>1) {
            lastPage = PageDao.getTxtContent((Integer) a.getMap().get("txtId"), a.getNowpage()-1);
        }else {
            lastPage = null;
        }

        if(a.getNowpage()<a.getPageCount()) {
            nextPage = PageDao.getTxtContent((Integer) a.getMap().get("txtId"), a.getNowpage()+1);
        }else {
            nextPage = null;
        }

        a.getHandler().sendEmptyMessage(1);
        //调用onDraw()方法
        super.postInvalidate();
    }
}
